var options = {
	'host': 'localhost',
	'port': '3306',
	'user': 'nodejs',
	'password': 'nodejs',
	'database': 'nodejs',
};

var mysql = require('mysql');
var pool = mysql.createPool(options);

/**
 * 释放数据库连接
*/
exports.release = function(connection) {
	connection.end(function(error) {
		console.log('Connection closed');
	});
};

exports.execQuery = function(options) {
	pool.getConnection(function(error, connection) {
		if(error) {
			console.log('DB-获取数据库连接异常！');
			throw error;
		}

		// 查询参数
		var sql = options['sql'];
		var args = options['args'];
		var handler = options['handler'];

		// 执行查询
		if(!args) {
			var query = connection.query(sql, function(error, results) {
				if(error) {
					console.log('DB-执行查询语句异常！');
					throw error;
				}

				// 处理结果
				handler(results);
			});
			console.log(query.sql);
		} else {
			var query = connection.query(sql, args, function(error, results) {
				if(error) {
					console.log('DB-执行查询语句异常！');
					throw error;
				}

				// 处理结果
				handler(results);
			});

			console.log(query.sql);
		}

		// 返回连接池
		connection.release(function(error) {
			if(error) {
				console.log('DB-关闭数据库连接异常！');
				throw error;
			}
		});
	});
};